$(function() {
	$(".add-file").click(function() {
		if($(".content-header").children("h1").html()=="编辑邮件"&&$(".help-block").attr("attid")!=undefined){
			layer.confirm('此附件将会替换原有的附件,确定继续添加？',{closeBtn: 0},function(index){				
				$(".file-input").trigger('click');
				layer.close(index);					
			});
		}else{
			$(".file-input").trigger('click');
		}	
	})
	$(".file-input").on("change",function(){
		var filePath=$(this).val();
		var arr=filePath.split('\\');
        var fileName=arr[arr.length-1];
        $(".filename").text(fileName);
        $(".file-att").text('');
	})
	//取消
	$(".mail-btn-cancel").click(function() {
		layer.open({
			type : 1,
			skin : 'demo-class',
			skin : 'yourclass',
			offset : '200px',
			title : '提示',
			area : [ '350px', '180px' ], // 宽高
			content : $('.prompt'),
			closeBtn : 0,
			btn : [ '离开并保存', '离开不保存', '取消' ],
			btnAlign : 'c',
			btn1: function(index, layero) {	
				sendandgraft("草稿");
				layer.close(index);	
				$(".get-box").trigger("click");										
			},
			btn2 : function(index, layero) {
				layer.close(index);	
				$(".get-box").trigger("click");			
			},
			btn3 : function(index, layero) {
				layer.close(index);
			}
		});
	
	})	
	
	//多个删除
	$(".btn-delete-many").click(function(){
		var obj=$(".icheckbox_flat-green-checked");
		var many=[];
		obj.each(function(){
			many.push($(this).attr("dataid"));
		})
		var box=$(".content-header").children("h1").html();
		deleteajax(many,box);
		deletepath($(this).attr("path"));	  
	})
	//设置已读
	$(".btn-isread").click(function(){
		var obj=$(".icheckbox_flat-green-checked");
		var many=[];
		obj.each(function(){
			$(this).removeClass("icheckbox_flat-green-checked");
			$(this).parent().find("email-tittle").css("font-weight","500");
			many.push($(this).attr("dataid"));
		})
		 $.ajax({
		  type:"post",
		  url:"/mail/manyisread",
		  cache:false,
		  async:false,
		  traditional:true,
		  data:{"many":many},
		  success:function(data){	 
		}
	  })
		  
	})
	//添加联系人
	$(".fancybox").click(function() {
		layer.open({
			type : 1,
			skin : 'demo-class',
			skin : 'yourclass',
			offset : '100px',
			title : '联系人',
			area : [ '804px', '492px' ], // 宽高
			content : $(".contacts-tables"),
			closeBtn : 0,
			btn : [ '确定', '取消' ],
			btnAlign : 'c',
			yes : function(index, layero) {
				$(".getmain-user").val("");
				$(".userid-input").val("");
				var obj=$(".icheckbox_flat-green-checked");
				obj.each(function(){				
					$(".getmain-user").val($(".getmain-user").val()+"、"+$(this).attr("name"));
					$(".userid-input").val($(".userid-input").val()+"、"+$(this).attr("data"));
				})
					var str=$(".getmain-user").val();
					var newstr=str.substr(1,str.length);				
					var idstr=$(".userid-input").val();
					var newidstr=idstr.substr(1,idstr.length);
					
					$(".getmain-user").val(" "+newstr);
					$(".userid-input").val(newidstr);
					layer.close(index);
			},
			btn2 : function(index, layero) {
				layer.close(index);
			}
		});
	})
	//收藏所有
	$(".star-togger").click(function(){
			var obj=$(".star-icon");
			var i=0;			
			var manystar=[];
			obj.each(function(){
				if($(this).children("i").hasClass("icon-collection_fill")){
					i++;
				}else{
					$(this).children("i").addClass("icon-collection_fill").removeClass("icon-collection");			
					manystar.push($(this).attr("dataid"));
				}		
			})			
			if(i==obj.length){		
				obj.children("i").removeClass("icon-collection_fill").addClass("icon-collection");
				obj.each(function(){
					manystar.push($(this).attr("dataid"));
				})
				setallstart("取消",manystar);
			}else{
				setallstart("收藏",manystar);
			}
		
		/*var manystar=[];
		var obj=$(".icheckbox_flat-green-checked");
			obj.each(function(){
				manystar.push($(this).attr("dataid"));	
			})*/
		})		
	//发送和存草稿
	$(".mail-btn-send").click(function(){
		var type=$(this).attr("type");
		var userid=$('.userid-input').val();
		if(userid==""){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>收件人不能为空！！！</span>\<\/div>";	 
			promptbox(obj);
			return;
		}else{
			sendandgraft(type);
			refreshPanel(getUrlPrefix("/mail"), "邮件管理");
		}
	})	
	//查看邮件回复
	$(".viewmail-btn-send ").click(function(){
		var senduserid=$(this).attr("sendid");
		var content=$(".back-textarea").val();
		
		 $.ajax({
			  type:"post",
			  url:"/mail/viewback",
			  cache:false,
			  async:false,
			  data:{"senduserid":senduserid,"content":content},
			  datatype:"json",
			  success:function(data){}
		  })	
		refreshPanel(getUrlPrefix("/mail"), "邮件管理");

	})	
	//发件箱信息刷新
	$(".send-box").click(function(){		
		refreshPanel(getUrlPrefix("/mail/sendbox"), "发件箱");
	})
	//收件箱信息刷新
	$(".get-box").click(function(){
		refreshPanel(getUrlPrefix("/mail/getbox"), "收件箱");
		$(".getorsenduser").html("发件人");
	})
	//草稿箱信息刷新
	$(".graft-box").click(function(){
		refreshPanel(getUrlPrefix("/mail/graftbox"), "草稿箱");
	})
	//垃圾箱刷新
	$(".gabege-box").click(function(){
		refreshPanel(getUrlPrefix("/mail/garbage"), "垃圾箱");
	})
	//星标邮件
	$(".email-star").click(function(){
		refreshPanel(getUrlPrefix("/mail/emailstar"), "星标邮件");
	})
	//编辑
	$(".email-edit").click(function(){
		refreshPanel(getUrlPrefix("/mail/edit?id="+$(this).attr("dataid")+""), "编辑邮件");
	})
	//查看界面
	$(".viewmail-btn").click(function(){
		refreshPanel(getUrlPrefix("/mail/view?id="+$(this).attr("dataid")+""), "查看邮件");
	})
	//完整模式回复
	$(".full-reback").click(function(){
		refreshPanel(getUrlPrefix("/mail/fullreback?senduser="+$(this).attr("senduser")+"&&sendid="+$(this).attr("sendid")+""), "添加邮件");
	})
	//查找指定人
	$(".search-btn-shareuser").click(function(){
		var inputval=$(".search-btn-shareuser-input").val();
		$(".contacts-tables tbody").html("");
		if(inputval==""){	
			newalluser();
			return;
		}
		 $.ajax({
			  type:"post",
			  url:"/file/searchshareuser",
			  cache:false,
			  async:false,
			  data:{"inputval":inputval},
			  datatype:"json",
			  success:function(data){
			  $.each(data,function(i,item){
				  newtable(item);		  
				})
			}
		  })
	})
	//单条信息删除
	$(".email-delete-icon").click(function(){
		var dataid=$(this).attr("dataid");
		var box=$(".content-header").children("h1").html();
		var many=[];
		many.push(dataid);
		deleteajax(many,box);
		deletepath($(this).attr("path"));
	})
	//全选
	$(".checkbox-togger").click(function(e){
			e.preventDefault();
			var num=0;
			var obj=$(".icheckbox");
			obj.each(function(){				
				if($(this).attr("aria-checked")=="false"){
					$(".icheckbox").addClass("icheckbox_flat-green-checked");
					$(".icheckbox").removeClass("icheckbox_flat-green");
					$(".icheckbox").attr("aria-checked","true");
				}else{
					num++;
				}		
			})
			if(num==obj.length){
				$(".icheckbox").addClass("icheckbox_flat-green");
				$(".icheckbox").removeClass("icheckbox_flat-green-checked");
				$(".icheckbox").attr("aria-checked","false");
			};
		});
	//已读
	$(".btn-isread").click(function(){
		
	})	
	//下载附件
	$(".dowload-att").click(function(){
		window.open("/mail/download/?attid="+$(this).attr("attid")+"");
	})
	//点击回复
	$(".back-div").click(function(){
		$(".full-reback").css("display","inline-block");
		$(".back-div-send-cancle").css("display","inline-block");
		$('.back-label').css("display","none");
		$('.back-textarea').slideDown();
	})
	//取消回复
	$(".view-back-canle").click(function(){		
		$('.back-label').css("display","block");
		$('.back-textarea').slideUp();
		$(".full-reback").css("display","none");
		$(".back-div-send-cancle").css("display","none");
	})
	//查找
	$(".search-btn").click(function(){
		var value=$(".search-input").val();
		if(value==""){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>查询不能为空！！！</span>\<\/div>";	 
			promptbox(obj);
			return;
		}
		if($(this).attr("path")=="/mail/garbage"){
			
			 refreshPanel(getUrlPrefix("/mail/search?type=garbage&&value="+value+""), "垃圾箱");
		 }else if($(this).attr("path")=="/mail/sendbox"){
			 
			  refreshPanel(getUrlPrefix("/mail/search?type=sendbox&&value="+value+""), "发件箱");
		 }else if($(this).attr("path")=="/mail/getbox"){			 
			  refreshPanel(getUrlPrefix("/mail/search?type=getbox&&value="+value+""), "收件箱");
		 }else if($(this).attr("path")=="/mail/graftbox"){
			 
			  refreshPanel(getUrlPrefix("/mail/search?type=graftbox&&value="+value+""), "草稿箱");
		 }else if($(this).attr("path")=="/mail/emailstar"){		 
			 refreshPanel(getUrlPrefix("/mail/search?type=emailstar&&value="+value+""), "星标邮件");			  
		}
	})
	})
//邮件选中
function select(e){
	if($(e).hasClass("icheckbox_flat-green-checked")){
		$(e).removeClass("icheckbox_flat-green-checked");
	}else{
		$(e).addClass("icheckbox_flat-green-checked")
	}	
}

/**
 * 是否收藏
 * @returns
 */
function isCollection(e){
	//判断是隐藏还是显示
	if($(e).find(".icon-collection_fill").length>0){
		//取消收藏
		$(e).find("i:first").removeClass("icon-collection_fill").addClass("icon-collection");
		var dataid=$(e).attr("dataid");
		var type="取消";
		setstart(dataid,type)
	}else{
		//收藏
		$(e).find("i:first").addClass("icon-collection_fill").removeClass("icon-collection");
		var dataid=$(e).attr("dataid");
		var type="收藏";
		setstart(dataid,type)
	}
}
//展开关闭点击事件
function jian(){
	if($(".yunpan").hasClass("s")){		
		$(".yunpan").slideDown();		
		$("#one").attr("class","iconfont icon-jian");
		$(".yunpan").removeClass("s");
	}else{			
		$(".yunpan").slideUp();
		$(".yunpan").addClass("s");
		$("#one").attr("class","iconfont icon-jia");
	}
};

//展开关闭点击事件
function jian2(e){
	if($(".leixing").hasClass("s")){
		$(".leixing").slideDown();
		$("#two").attr("class","iconfont icon-jian");
		$(".leixing").removeClass("s");
	}else{
		$(".leixing").slideUp();
		$(".leixing").addClass("s");
		$("#two").attr("class","iconfont icon-jia");
	}
};

function newtable(item){
	var tr=$("<tr style='border-bottom: 1px solid #f2f2f2'></tr>")
	var td1=$("<td></td>")
	var div=$("<div  data="+item.id+" name="+item.realName+" onclick='select(this)' class='icheckbox icheckbox_flat-green'aria-checked=false style='position:relative;'></div>")
	var input=$("<input type='checkbox' style='position: absolute; top: -20%;left: -20%;display: block;width: 140%; height: 140%;margin: 0px;padding: 0px;background: rgb(255, 255, 255);border: 0px;opacity: 0;'>")	
	var ins=$("<ins class='iCheck-helper' style='position: absolute;top: -20%;left: -20%;display: block;width: 140%;height: 140%; margin: 0px;padding: 0px;background: rgb(255, 255, 255);border: 0px;opacity: 0;'></ins>")
	var td2=$("<td><img src='../images/"+item.photo+"' style='border-radius: 50%;width: 25px; height: 25px; vertical-align: middle;'/></td>");
	var td3=$("<td>"+item.dept.name+"</td>");
	var td4=$("<td>"+item.realName+"</td>");
	var td5=$("<td>"+item.deptOffice.name+"</td>");
	var td6=$("<td>"+item.tel+"</td>");
	div.append(input);
	div.append(ins);
	td1.append(div);
	tr.append(td1);
	tr.append(td2);
	tr.append(td3);
	tr.append(td4);
	tr.append(td5);
	tr.append(td6);
	$(".contacts-tables tbody").append(tr);	
}
function newalluser(){
	 $.ajax({
		  type:"post",
		  url:"/file/user",
		  cache:false,
		  async:false,
		  datatype:"json",
		  success:function(data){
		  $.each(data,function(i,item){
			  newtable(item);
			})
		}
	  })
}

//发送和存草稿方法
function sendandgraft(type){
	var attid;
	var fordata=new FormData();
	var forfile=new FormData();
	var username=$(".getmain-user").val();
	var statustype=$(".statustype").val();
	var tittle=$(".email-tittle").val();
	var userid=$('.userid-input').val();
	var content=CKEDITOR.instances.TextArea1.getData();
	fordata.append("statustype",statustype);
	fordata.append("tittle",tittle);
	fordata.append("userid",userid);
	fordata.append("content",content);
	fordata.append("username",username);
	fordata.append("type",type);

	if($(".file-input")[0].files[0]!=undefined){
		forfile.append("file",$(".file-input")[0].files[0]);
		$.ajax({
			type:"post",
			url:"/mail/uploadfile",
			data:forfile,
			processData:false,
			contentType:false,
			cache:false,
			async:false,
			datatype:"json",
			success:function(data){	
				attid=data;
			}
		})
	}
	if(attid==undefined){
		fordata.append("attid","undefined");
	}else if(attid==undefined &&$(".content-header").children("h1").html()=="编辑邮件"){
		if($(".help-block").attr("attid")!=undefined){
			attid=$(".help-block").attr("attid");
			fordata.append("attid",attid);
		}else{
			fordata.append("attid","undefined");
		}	
	}else{
		fordata.append("attid",attid);
	}
	$.ajax({
		type:"post",
		url:"/mail/sendemail",
		data:fordata,		
		cache:false,
		async:false,
		processData:false,
		contentType:false,
		success:function(){			
		}
	})
	
	
}
/**
 * 收藏和取消收藏
 */
function setstart(dataid,type){
	$.ajax({
		type:"post",
		url:"/mail/setstart",
		cache:false,
		async:false,
		data:{"dataid":dataid,"type":type},
		success:function(){		
		}
	})
}
/**
 * 收藏所有和取消所有
 */
function setallstart(type,many){
	$.ajax({
		type:"post",
		url:"/mail/setallstart",
		cache:false,
		async:false,
		traditional: true,
		data:{"type":type,"many":many},
		success:function(){		
		}
	})
}
//提示框
function promptbox(content){
	  layer.open({ 
		  type: 1,		
		  skin: 'demo-class',
		  skin: 'yourclass',
		  offset: '100px',
		  title :'提示',
		  area: ["300px","200px"], //宽高
		  content: content,
		  closeBtn: 0,
		  btn:['关闭'],
		  btnAlign: 'c', 
		  btn1: function(index, layero){
			layer.close(index);
		    //按钮【按钮二】的回调		    
		    //return false 开启该代码可禁止点击该按钮关闭
		  }
		})
}
//删除调用的ajax
function deleteajax(many,box){
	 $.ajax({
		  type:"post",
		  url:"/mail/deletemany",
		  cache:false,
		  async:false,
		  traditional:true,
		  data:{"many":many,"box":box},
		  success:function(data){
		  $.each(data,function(i,item){	  
			})
		}
	  })
}
function deletepath(path){
	if(path=="/mail/garbage"){
		 $(".gabege-box").click();
	 }else if(path=="/mail/sendbox"){
		  $(".send-box").click();
	 }else if(path=="/mail/getbox"){
		  $(".get-box").click();
	 }else if(path=="/mail/graftbox"){
		  $(".graft-box").click();
	 }else if(path=="/mail/emailstar"){
		  $(".email-star").click();
	}
}
